package com.sg.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.sg.utils.ConvertUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liwei
 */
@Data
public class LgPage<T> {
    /**
     * 总条数
     */
    private long total;

    /**
     * 总页数
     */
    private long totalPage;

    /**
     * 当前页
     */
    private long current;

    /**
     * 当前页条数
     */
    private long currentNum;

    /**
     * 记录集合
     */
    private List<T> records = new ArrayList<>();

    /**
     * 额外信息
     */
    private Object extra;


    /**
     * 将pagehelper查询结果转成分页对象
     * @param list
     * @param <T>
     * @return
     */
    public static <T> LgPage<T> setPage(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords(pageInfo.getList());
        result.setTotalPage(pageInfo.getPages());
        return result;
    }

    /**
     * 将pagehelper查询结果转成分页对象，附加extra
     * @param list
     * @param <T>
     * @return
     */
    public static <T> LgPage<T> setPage(List<T> list,Object extra) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords(pageInfo.getList());
        result.setTotalPage(pageInfo.getPages());
        result.setExtra(extra);
        return result;
    }

    /**
     * 将pagehelper查询结果转成指定vo
     * @param list pagehelper查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<?> setPage(List<T> list, Class<?> target) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        List<T> infoList = pageInfo.getList();
        List<?> voList = ConvertUtil.entityToVoList(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords((List<T>) voList);
        result.setTotalPage(pageInfo.getPages());
        return result;
    }

    /**
     * 将pagehelper查询结果转成指定vo,附加额外信息extra
     * @param list pagehelper查询的集合
     * @param target vo实体
     * @param extra 额外信息
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<?> setPage(List<T> list, Class<?> target, Object extra) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        List<T> infoList = pageInfo.getList();
        List<?> voList = ConvertUtil.entityToVoList(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords((List<T>) voList);
        result.setTotalPage(pageInfo.getPages());
        result.setExtra(extra);
        return result;
    }


    /**
     * 将IPage查询结果转成指定vo
     * @param page IPage查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<T> setPage(IPage<?> page, Class<T> target) {
        List<?> infoList = page.getRecords();
        List<T> voList = ConvertUtil.entityToVoList(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setCurrentNum(page.getSize());
        result.setRecords(voList);
        result.setTotalPage(page.getPages());
        return result;
    }


    /**
     * 将IPage查询结果转成指定vo
     * @param page IPage查询的集合
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<T> setPage(IPage<T> page) {
        LgPage<T> result = new LgPage<>();
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setCurrentNum(page.getSize());
        result.setRecords(page.getRecords());
        result.setTotalPage(page.getPages());
        return result;
    }
    /**
     * 将pagehelper查询结果转成指定vo
     * 可将Entity中的字典字段转为枚举
     *
     * @param list pagehelper查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<?> setPageEnum(List<T> list, Class<?> target) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        List<T> infoList = pageInfo.getList();
        List<?> voList = ConvertUtil.entityToVoListEnum(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords((List<T>) voList);
        result.setTotalPage(pageInfo.getPages());
        return result;
    }

    /**
     * 将pagehelper查询结果转成指定vo,附加额外信息extra
     * 可将Entity中的字典字段转为枚举
     *
     * @param list pagehelper查询的集合
     * @param target vo实体
     * @param extra 额外信息
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<?> setPageEnum(List<T> list, Class<?> target, Object extra) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        List<T> infoList = pageInfo.getList();
        List<?> voList = ConvertUtil.entityToVoListEnum(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords((List<T>) voList);
        result.setTotalPage(pageInfo.getPages());
        result.setExtra(extra);
        return result;
    }


    /**
     * 将IPage查询结果转成指定vo
     * 可将Entity中的字典字段转为枚举
     *
     * @param page IPage查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<T> setPageEnum(IPage<?> page, Class<T> target) {
        List<?> infoList = page.getRecords();
        List<T> voList = ConvertUtil.entityToVoListEnum(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setCurrentNum(page.getSize());
        result.setRecords(voList);
        result.setTotalPage(page.getPages());
        return result;
    }
    /**
     * 将pagehelper查询结果转成指定vo
     * 可将Entity中的字典字段转为枚举
     *
     * @param list pagehelper查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<?> setPageSimple(List<T> list, Class<?> target) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        List<T> infoList = pageInfo.getList();
        List<?> voList = ConvertUtil.entityToVoListSimple(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords((List<T>) voList);
        result.setTotalPage(pageInfo.getPages());
        return result;
    }

    /**
     * 将pagehelper查询结果转成指定vo,附加额外信息extra
     * 可将Entity中的字典字段转为枚举
     *
     * @param list pagehelper查询的集合
     * @param target vo实体
     * @param extra 额外信息
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<?> setPageSimple(List<T> list, Class<?> target, Object extra) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        List<T> infoList = pageInfo.getList();
        List<?> voList = ConvertUtil.entityToVoListSimple(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords((List<T>) voList);
        result.setTotalPage(pageInfo.getPages());
        result.setExtra(extra);
        return result;
    }


    /**
     * 将IPage查询结果转成指定vo
     * 可将Entity中的字典字段转为枚举
     *
     * @param page IPage查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<T> setPageSimple(IPage<?> page, Class<T> target) {
        List<?> infoList = page.getRecords();
        List<T> voList = ConvertUtil.entityToVoListSimple(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setCurrentNum(page.getSize());
        result.setRecords(voList);
        result.setTotalPage(page.getPages());
        return result;
    }


    /**
     * 将IPage查询结果转成指定vo
     * @param page IPage查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<T> setPageByJson(IPage<?> page, Class<T> target) {
        List<?> infoList = page.getRecords();
        List<T> voList = BeanConvertUtil.convertListByJson(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        result.setCurrentNum(page.getSize());
        result.setRecords(voList);
        result.setTotalPage(page.getPages());
        return result;
    }


    /**
     * 将查询结果转成指定vo
     * @param list 查询的集合
     * @param target vo实体
     * @param <T>
     * @return 返给前端的vo数据
     */
    public static <T> LgPage<T> setPageByJson(List<?> list, Class<T> target) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        List<?> infoList = pageInfo.getList();
        List<T> voList = BeanConvertUtil.convertListByJson(infoList, target);
        LgPage<T> result = new LgPage<>();
        result.setTotal(pageInfo.getTotal());
        result.setCurrent(pageInfo.getPageNum());
        result.setCurrentNum(list.size());
        result.setRecords(voList);
        result.setTotalPage(pageInfo.getPages());
        return result;
    }

}
